<link href="asset/css/show.css" rel="stylesheet" type="text/css">
<script src="asset/js/cetak_excel.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#show").hide();
	$("#sembunyi").hide();
	$("#tampil").show();
	
	$("#tampil").click(function(){
		$("#show").show(500);
		$("#tampil").hide();
		$("#sembunyi").show();
	});
	$("#sembunyi").click(function(){
		$("#show").hide(500);
		$("#sembunyi").hide();
		$("#tampil").show();
	});
	
	$("#periode").change(function(){
		var periode = $("#periode").val();
		if(periode == 1){
			document.getElementById('tgl1').disabled = false;
			document.getElementById('tgl2').disabled = false;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}
		else if(periode == 2){
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}else{
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = true;
			document.getElementById('bln2').disabled = true;
		}
	});
});
</script>
<?php
	$tgl = date("d");
	$bln = date("m");
	$thn = date("Y");
	
	$periode = $_POST['periode'];
	$tgl1 = $_POST['tgl1'];
	$tgl2 = $_POST['tgl2'];
	$bln1 = $_POST['bln1'];
	$bln2 = $_POST['bln2'];
	$thn1 = $_POST['thn1'];
	$thn2 = $_POST['thn2'];
	$jns = $_POST['jenis'];
	$wlyh = $_POST['wilayah'];
?>
<div id="tiga_ba">
	<div id="tiga_baa">Laporan Rekap Peserta </div>
	<div id="tiga_bab">
		<!--<a href=""><img src="asset/images/print.png" align="right" width="25"></a>-->
	</div>
	<div class="clear"></div>
</div>

<form method=post action='utama.php?module=mod_rekap&view=peserta'>
	<table style="margin-left: 5px;">
		<tr>
			<td width="75">Periode </td>
			<td width="300">
				<select name="periode" id="periode" class="input1">
					<?php
						if($periode == 1){
							$s1 = "selected";
						}else if($periode == 2){
							$p1 = "disabled";
							$s2 = "selected";
						}else{
							$p1 = "disabled";
							$p2 = "disabled";
							$s3 = "selected";
						}
					?>
					<option value="1" <?php echo $s1; ?>>Hari</option>
					<option value="2" <?php echo $s2; ?>>Bulan</option>
					<option value="3" <?php echo $s3; ?>>Tahun</option>
				</select>
			</td>
			<td width="100"></td>
			<td width="300"></td>
		</tr>
		<tr>
			<td>Dari </td>
			<td>
				<select name="tgl1" id="tgl1" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl1 == ''){
								$s = "selected";
							}else if($i == $tgl1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln1" id="bln1" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln1 == ''){
								$s = "selected";
							}else if($i == $bln1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn1" id="thn1" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn1 == ''){
								$s = "selected";
							}else if($i == $thn1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
			<td>Jenis Peserta</td>
			<td>
				<select name="jenis" id="jenis" class="input2">
					<option value=""> - - Pilih Semua - - </option>
					<?php
						$jenis = mysql_query("select * from jenis_peserta order by jenis_peserta");
						while($j = mysql_fetch_array($jenis)){
							if($jns == $j['id_jenis_peserta']){
								$sj = "selected";
							}else{
								$sj = "";
							}
							echo "<option value=$j[id_jenis_peserta] $sj>$j[jenis_peserta]</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Sampai </td>
			<td>
				<select name="tgl2" id="tgl2" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl2 == ''){
								$s = "selected";
							}else if($i == $tgl2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln2" id="bln2" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln2 == ''){
								$s = "selected";
							}else if($i == $bln2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn2" id="thn2" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn2 == ''){
								$s = "selected";
							}else if($i == $thn2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
			<td>Wilayah</td>
			<td>
				<select name="wilayah" id="wilayah" class="input2">
					<option value=""> - - Pilih Semua - - </option>
					<?php
						$wilayah = mysql_query("select * from kecamatan where id_kabupaten = '6405' order by kecamatan");
						while($w = mysql_fetch_array($wilayah)){
							if($wlyh == $w['id_kecamatan']){
								$sw = "selected";
							}else{
								$sw = "";
							}
							echo "<option value=$w[id_kecamatan] $sw>$w[kecamatan]</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><input name="submit" id="proses" type="submit" class="button" value="Proses" /></td>
			<td></td>
			<td></td>
		</tr>
	</table>
</form>
<?php
	if($periode == 1){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-$tgl1'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-$tgl2'";
	}else if($periode == 2){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-31'";
	}else{
		$tgl1 = "and p.tgl_mulai >= '$thn1-01-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-12-31'";
	}
	
	if($jns == ''){
		$hsl_jns = "";
	}else{
		$hsl_jns = "and p.id_jenis_peserta = $jns";
	}
	
	if($wlyh == ''){
		$sql="select kc.id_kecamatan, kc.kecamatan,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa where d.id_kecamatan = kc.id_kecamatan $hsl_jns $tgl1 $tgl2)  as jml from kecamatan kc where id_kabupaten = '6405'";
	}else{
		$sql="select ds.id_desa, ds.desa,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa where d.id_desa = ds.id_desa $hsl_jns $tgl1 $tgl2)  as jml from desa ds where ds.id_kecamatan = '$wlyh'";
	}
	
	if($_POST['submit'] == 'Proses'){
	?>
		<div id="btnChart">
		<span id="tampil"><img src="asset/images/chart.png" width="30"></span>
		<span id="sembunyi"><img src="asset/images/chart2.png" width="30"></span>
		<span id="print">
			<a href="#" class="export">
				<img src="asset/images/print.png" width="30">
			</a>
		</span>
	</div>
	<div id="show">
		<script type="text/javascript">
			$(function () {
				$('#container').highcharts({
					data: {
						table: document.getElementById('datatable')
					},
					chart: {
						type: 'column'
					},
					title: {
						text: 'Grafik Data Peserta'
					},
					yAxis: {
						allowDecimals: false,
						title: {
							text: 'Jumlah Peserta'
						}
					},
					tooltip: {
						formatter: function() {
							return '<b>'+ this.series.name +'</b><br/>'+
								this.y +' '+ this.x.toLowerCase();
						}
					}
				});
			});
		</script>
		<script src="chart/highcharts.js"></script>
		<script src="chart/modules/data.js"></script>
		<script src="chart/modules/exporting.js"></script>
		<div id="container" style="width: 790px; height: 400px; margin: 0 auto"></div>
	</div>
		<!------------------------------------------------------------------->
		<table class="tabel" id="datatable" style="display: none;">
			<thead>
				<tr>
					<!--<th>NO</th>-->
					<th>Nama</th>
					<th>Peserta</th>
				</tr>
				</thead>
			<tbody>
				<?php
					//echo $sql;
					$qry=mysql_query($sql);
						while($hasil=mysql_fetch_array($qry)){
				?>
							<tr>
								<!--<td align="center" width="30"><?php echo $no;?></td>-->
								<td >
									<?php 
									if($wlyh == ''){
										echo $hasil['kecamatan'];
									}else{
										echo $hasil['desa'];
									}
								?>
								</td>
								<td align="center" align="center" width="120"><?php echo $hasil['jml'];?></td>
							</tr>
							
					<?php
						}
					?>
				</tbody>
			</table>
		<div id="dvData">
		<table class="tabel">
			<tr>
				<th>NO</th>
				<th>Nama Wilayah</th>
				<th>Jumlah</th>
			</tr>
			<?php
				//echo $sql;
				$qry=mysql_query($sql);
					while($hasil=mysql_fetch_array($qry)){
						$no++;
			?>
						<tr>
							<td align="center" width="30"><?php echo $no;?></td>
							<td >
								<?php 
									if($wlyh == ''){
										echo $hasil['kecamatan'];
									}else{
										echo $hasil['desa'];
									}
								?>
							</td>
							<td align="center" align="center" width="60"><?php echo $hasil['jml'];?></td>
						</tr>
						
				<?php
						$jumlah = $jumlah + $hasil['jml'];
					}
				?>
			<tr>
				<td align="center" colspan="2"><b>Jumlah</b></td>
				<td align="center" align="center" width="60"><?php echo "<b>$jumlah</b>";?></td>
			</tr>
		</table>
		</div>
	<?php
	}
	?>
